package com.aneliya.io.xml;

import org.apache.xerces.dom.DOMImplementationImpl;
import org.w3c.dom.CDATASection;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.aneliya.model.ErrorType;
import com.aneliya.model.Message;
import com.aneliya.model.User;

public class XMLMessageFactory {
	
	public Document createXMLMessage(Message message) {
		switch (message.getMessageType()) {
		case TEXT:
			return createTextXMLMessage(message);
		case REGISTER:
			return createRegisterXMLMessage(message);
		case LOGIN:
			return createLoginXMLMessage(message);
		case LOGOUT:
			return createLogoutXMLMessage(message);
		case ERROR:
			return createErrorXMLMessage(message);
		case KEEP_ALIVE:
			return createKeepAliveXMLMessage(message);

		default:
			return null;
		}
	}

	private Document createTextXMLMessage(Message message) {
		DOMImplementation domImplementation = DOMImplementationImpl.getDOMImplementation();
		
		Document xmlMessage = domImplementation.createDocument(null, "message", null);
		Element messageElement = xmlMessage.getDocumentElement();
		
		messageElement.setAttribute("type", message.getMessageType().toString());
		messageElement.setAttribute("username", message.getUsername());
		
		Element tokenElement = xmlMessage.createElement("token");
		CDATASection tokenCDATA = xmlMessage.createCDATASection(
				message.getToken().toString());
		tokenElement.appendChild(tokenCDATA);
		
		messageElement.appendChild(tokenElement);
		
		Element messageDataElement = xmlMessage.createElement("message_data");
		CDATASection messageCDATA = xmlMessage.createCDATASection(
				(String) message.getMessage());
		messageDataElement.appendChild(messageCDATA);
		
		messageElement.appendChild(messageDataElement);
		
		return xmlMessage;
	}

	private Document createRegisterXMLMessage(Message message) {
		DOMImplementation domImplementation = DOMImplementationImpl.getDOMImplementation();
		
		Document xmlMessage = domImplementation.createDocument(null, "message", null);
		Element messageElement = xmlMessage.getDocumentElement();
		
		messageElement.setAttribute("type", message.getMessageType().toString());
		
		Element userElement = xmlMessage.createElement("user");
		User user = (User) message.getMessage();
		
		Element usernameElement = xmlMessage.createElement("username");
		CDATASection usernameCDATA = xmlMessage.createCDATASection(user.getUsername());
		usernameElement.appendChild(usernameCDATA);
		userElement.appendChild(usernameElement);
		
		Element passwordElement = xmlMessage.createElement("password");
		CDATASection passwordCDATA = xmlMessage.createCDATASection(user.getPassword());
		passwordElement.appendChild(passwordCDATA);
		userElement.appendChild(passwordElement);
		
		messageElement.appendChild(userElement);
		
		return xmlMessage;
	}

	private Document createLoginXMLMessage(Message message) {
		User user = (User) message.getMessage();
		if (user != null) {
			return createRegisterXMLMessage(message);
		}
		
		DOMImplementation domImplementation = DOMImplementationImpl.getDOMImplementation();
		
		Document xmlMessage = domImplementation.createDocument(null, "message", null);
		Element messageElement = xmlMessage.getDocumentElement();
		
		messageElement.setAttribute("type", message.getMessageType().toString());
		messageElement.setAttribute("username", message.getUsername());
		
		return xmlMessage;
	}

	private Document createLogoutXMLMessage(Message message) {
		DOMImplementation domImplementation = DOMImplementationImpl.getDOMImplementation();
		
		Document xmlMessage = domImplementation.createDocument(null, "message", null);
		Element messageElement = xmlMessage.getDocumentElement();
		
		messageElement.setAttribute("type", message.getMessageType().toString());
		messageElement.setAttribute("username", message.getUsername());
		
		return xmlMessage;
	}

	private Document createErrorXMLMessage(Message message) {
		DOMImplementation domImplementation = DOMImplementationImpl.getDOMImplementation();
		
		Document xmlMessage = domImplementation.createDocument(null, "message", null);
		Element messageElement = xmlMessage.getDocumentElement();
		
		messageElement.setAttribute("type", message.getMessageType().toString());
		
		ErrorType errorType = (ErrorType) message.getMessage();
		messageElement.setAttribute("error_type", errorType.toString());
		
		return xmlMessage;
	}

	private Document createKeepAliveXMLMessage(Message message) {
		DOMImplementation domImplementation = DOMImplementationImpl.getDOMImplementation();
		
		Document xmlMessage = domImplementation.createDocument(null, "message", null);
		Element messageElement = xmlMessage.getDocumentElement();
		
		messageElement.setAttribute("type", message.getMessageType().toString());
		
		return xmlMessage;
	}
	
}
